#include <cstring>
#include <iostream>
#include <ostream>

/*

   给字符串类型添加迭代器的实现
*/
class String {
public:
    String(const char* p = nullptr) {
        if (p) {
            _pdata = new char[strlen(p) + 1];
            strcpy(_pdata, p);
        } else {
            _pdata = new char[1];
            *_pdata = 0;
        }
    }

    ~String() {
        delete[] _pdata;
        _pdata = nullptr;
    }

    String(const String& other) {
        _pdata = new char[strlen(other._pdata) + 1];
        strcpy(_pdata, other._pdata);
    }

    String(String&& other) {
        _pdata = other._pdata;
        other._pdata = nullptr;
    }

    String& operator=(const String& other) {
        if (this == &other) return *this;
        delete[] _pdata;
        _pdata = new char[strlen(other._pdata) + 1];
        strcpy(_pdata, other._pdata);
        return *this;
    }

    String& operator=(String&& other) {
        if (this == &other) return *this;
        delete[] _pdata;
        _pdata = other._pdata;
        other._pdata = nullptr;
        return *this;
    }

    class iterator {
    public:
        iterator(char* p = nullptr) : _p(p) {
        }

        bool operator!=(const iterator& it) {
            return _p != it._p;
        }

        void operator++() {
            _p++;
        }

        char& operator*() {
            return *_p;
        }

    private:
        char* _p;
    };

    iterator begin() {
        return iterator(_pdata);
    }
    iterator end() {
        return iterator(_pdata + length());
    }

public:
    bool operator>(const String& other) {
        return strcmp(_pdata, other._pdata) > 0;
    }

    bool operator<(const String& other) {
        return strcmp(_pdata, other._pdata) < 0;
    }

    bool operator==(const String& other) {
        return strcmp(_pdata, other._pdata) == 0;
    }

    char& operator[](size_t idx) {
        return _pdata[idx];
    }

    const char& operator[](size_t idx) const {
        return _pdata[idx];
    }

    size_t length() const {
        return strlen(_pdata);
    }

    const char* c_str() const {
        return _pdata;
    }

private:
    char* _pdata;
};

String operator+(const String& a, const String& b) {
    char* ptmp = new char[strlen(a.c_str()) + strlen(b.c_str()) + 1];
    strcpy(ptmp, a.c_str());
    strcat(ptmp, b.c_str());
    String tmp(ptmp);
    delete[] ptmp;
    return tmp;
}

std::ostream& operator<<(std::ostream& os, const String& val) {
    os << val.c_str();
    return os;
}

int main() {
    String s;
    String s2("hello,world");
    String s3(s2);
    s = s2;
    std::cout << s << " " << s2 << " " << s3 << std::endl;
    std::cout << s.c_str() << " " << s2.c_str() << " " << s3.c_str()
              << std::endl;
    std::cout << s.length() << " " << s2.length() << " " << s3.length()
              << std::endl;
    std::cout << (s < s2) << " " << (s > s3) << " " << (s2 == s3) << std::endl;
    String s4 = s2 + s3;
    std::cout << s4 << std::endl;
    std::cout << "==========" << std::endl;

    for (auto c : s4) {  // 使用 begin()和end()方法
        std::cout << c << " ";
    }
}
